package com.test.client.customTimeLine;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Element;
import com.netthreads.gwt.simile.timeline.client.TimelineXMLHandler;

class CustomTimeLineImpl {

	public native static CustomTimeLine create(JavaScriptObject bands, Element divElement) /*-{
		var t =$wnd.Timeline.create(divElement, bands);
		return t;
	}-*/;

	public native static void loadXML(String dataUrl, TimelineXMLHandler handler) /*-{
		$wnd.Timeline.loadXML(dataUrl, function(xml, url) { handler.@com.netthreads.gwt.simile.timeline.client.TimelineXMLHandler::onCompletion(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;)(xml,url) });
	}-*/;

	public native static int getBandCount(CustomTimeLine timeLine) /*-{
		return timeLine.getBandCount();
	}-*/;

	public native static void layout(CustomTimeLine timeLine) /*-{
		timeLine.layout();
	}-*/;

	public native static void closeBubble(int index, CustomTimeLine timeLine) /*-{
		timeLine.getBand(index).closeBubble();
	}-*/;

	public native BandInfo getBand(int index)/*-{
		return $wnd.Timeline.getBand(index);
	}-*/;

	public native static void addScrollListener(int index, CustomTimeLine timeLine)/*-{
		timeLine.getBand(index).addOnScrollListener($wnd.bandScroll);
	}-*/;

	public native static void registerCustomeEventClickHandler()/*-{
		$wnd.Timeline.OriginalEventPainter.prototype._showBubble = $wnd.eventClicked;
	}-*/;

	public native static void clearTimeline(CustomTimeLine timeLine)/*-{
		timeLine.getBand(0).getEventSource().clear();
	}-*/;

	public native static void renderEvents(JavaScriptObject events, CustomTimeLine timeline)/*-{
		timeline.getBand(0).getEventSource().addMany(events);
	}-*/;

	public static native void removeEventsBeforeDate(double date, CustomTimeLine timeline)/*-{
		var d=new Date(date);
		var es=timeline.getBand(0).getEventSource();
		var earliestDate=es.getEarliestDate();
		if(!es) return;
		var		eventIterator=es.getEventIterator(earliestDate,d);
		while(eventIterator && eventIterator.hasNext()){
		var evt = eventIterator.next();
		if(evt && es._events && es._events._events){
		es._events._events.remove(evt);
		}
		}
		es._fire("onClear", []);
	}-*/;

	public native static void removeEventsAfterDate(double date, CustomTimeLine timeLine)/*-{
		var d=new Date(date);		
		var es=timeLine.getBand(0).getEventSource();
		if(!es) return;
		var latestDate=es.getLatestDate();				

		var eventIterator=es.getEventIterator(d,latestDate);
		while(eventIterator && eventIterator.hasNext()){

		var evt = eventIterator.next();
		if(evt && es._events && es._events._events ){
		es._events._events.remove(evt);
		}
		}
		es._fire("onClear", []);
	}-*/;

	public static native double getMinVisibleDate(int bandIndex, CustomTimeLine timeline)/*-{
		band=timeline.getBand(bandIndex);
		return band.getMinVisibleDate().getTime();
	}-*/;

	public static native double getMaxVisibleDate(int bandIndex, CustomTimeLine timeline)/*-{
		band=timeline.getBand(bandIndex);
		return band.getMaxVisibleDate().getTime();
	}-*/;

	public static native void setTimeLineBandZeroHeight(int timelineBandZeroHeight, CustomTimeLine customTimeLine) /*-{
		if($wnd.document.getElementById("timeline-band-0") == null) 
		return;
		$wnd.document.getElementById("timeline-band-0").style['height']=timelineBandZeroHeight;
	}-*/;

}